package com.credithc.eagleeye.config;
import com.credithc.framework.es.util.ElasticSearchConfiguration;
import com.credithc.framework.es.util.ElasticSearchUtil;
import com.credithc.framework.redis.util.RedisClusterUtil;
import com.credithc.framework.redis.util.RedisConfiguration;
import com.credithc.framework.redis.util.RedisSingleUtil;
import com.credithc.framework.redis.util.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
 * 描 述: <br/>
 * 作 者: zhangjin<br/>
 * 创 建：2017年09月14日<br/>
 * 版 本：v1.0.0<br>
 * <p>
 * 历 史: (版本) 作者 时间 注释 <br/>
 */
@Configuration
@Slf4j
public class SpringBeansConfig {
    @Bean
    public ElasticSearchUtil esUtilBean(EagleMonitorESConfig eagleMonitorESConfig){
        ElasticSearchConfiguration esConfig=new ElasticSearchConfiguration();
        esConfig.setAddress(eagleMonitorESConfig.getAddress());
        esConfig.setClusterName(eagleMonitorESConfig.getClusterName());
        esConfig.setShieldUser(eagleMonitorESConfig.getShieldUser());
        ElasticSearchUtil esUtil = new ElasticSearchUtil(esConfig);
        log.info(String.format("eagle-eye-transfer传输监控ES地址:%s,clusterName:%s,shieldUser:%s",eagleMonitorESConfig.getAddress(),eagleMonitorESConfig.getClusterName(),eagleMonitorESConfig.getShieldUser()));
        return esUtil;
    }

    @Bean
    public RedisUtil redisBean(EagleMonitorRedisConfig eagleMonitorRedisConfig){
        RedisUtil redisUtil=null;
        if(eagleMonitorRedisConfig.isEnable()) {
            RedisConfiguration redisConfiguration = new RedisConfiguration();
            redisConfiguration.setAddress(eagleMonitorRedisConfig.getAddress());
            redisConfiguration.setDatabase(eagleMonitorRedisConfig.getDatabase());
            redisConfiguration.setMaxIdle(eagleMonitorRedisConfig.getMaxIdle());
            redisConfiguration.setMaxRedirections(eagleMonitorRedisConfig.getMaxRedirections());
            redisConfiguration.setMaxTotal(eagleMonitorRedisConfig.getMaxTotal());
            redisConfiguration.setMinIdle(eagleMonitorRedisConfig.getMinIdle());
            redisConfiguration.setMaxWaitMillis(eagleMonitorRedisConfig.getMaxWaitMillis());
            redisConfiguration.setMode(eagleMonitorRedisConfig.getMode());
            redisConfiguration.setTimeout(eagleMonitorRedisConfig.getTimeout());
            log.info("eagle-eye-transfer:redis传输通道已开启,redis地址:{}", redisConfiguration.getAddress());
            if (eagleMonitorRedisConfig.getMode() == 1) {
                redisUtil = new RedisSingleUtil(redisConfiguration);
            }else{
                redisUtil = new RedisClusterUtil(redisConfiguration);
            }
        }else {
            log.warn("eagle-eye-transfer:redis传输通道未开启");
        }
        return redisUtil;
    }
}
